package com.llkj.qingnianguoxue.circle;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.base.BaseFragment;
import com.llkj.qingnianguoxue.base.ConstantUserInfo;
import com.llkj.qingnianguoxue.base.MyApplication;
import com.llkj.qingnianguoxue.login.LoginActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 圈层
 */
public class CircleFragment extends BaseFragment implements View.OnClickListener{
    @BindView(R.id.fl_interest_circle)
    protected FrameLayout flInterest;//兴趣圈
    @BindView(R.id.ll_my_circle)
    protected FrameLayout flMineCircle;//我的圈
    @BindView(R.id.ll_near_people)
    protected FrameLayout flNearPeople;//附近的人

    private InterestCircleFragment interestCircleFragment;//兴趣圈
    private MyCircleFragment myCircleFragment;//我的圈子
    private NearPeopleFragment mNearPeopleFragment;//附近的人

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_circle, container, false);
        setTitle("圈子",true,TITLE_TYPE_IMG, R.drawable.icon_cirlce_add,true,TITLE_TYPE_IMG, R.drawable.icon_circle_search);
        ButterKnife.bind(this,rootView);

        initView();
        initListener();

        setSelection(0);
        return rootView;
    }

    private void initView(){
        rlTitle.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.colorFE4A49));
        tvTitle.setTextColor(ContextCompat.getColor(getActivity(), R.color.white));
    }

    private void initListener(){
        flInterest.setOnClickListener(this);
        flMineCircle.setOnClickListener(this);
    }

    @Override
    protected void rightDoWhat() {
        super.rightDoWhat();
        startActivity(SearchCircleActivity.class);
    }

    @Override
    protected void leftDoWhat() {
        boolean isLogin = MyApplication.getInstance().sputil.getBoolean(ConstantUserInfo.ISLOGIN);
        if (!isLogin) {
            dialogConfirm(null, null, null, null).show();
            return;
        }
        startActivity(CreateCircleActivity.class);
    }

    @Override
    protected void dialogLeftBtn() {
        super.dialogLeftBtn();
    }

    @Override
    protected void dialogRightBtn() {
        startActivity(LoginActivity.class);
    }

    @OnClick({R.id.ll_near_people, R.id.fl_interest_circle, R.id.ll_my_circle})
    public void onClick(View v){
        boolean isLogin = MyApplication.getInstance().sputil.getBoolean(ConstantUserInfo.ISLOGIN);
        switch (v.getId()){
            case R.id.ll_near_people:
                if (!isLogin) {
                    dialogConfirm(null, null, null, null).show();
                    return;
                }
                setSelection(2);
//                startActivity(NearPeopleActivity.class);
                break;
            case R.id.fl_interest_circle://兴趣
                setSelection(0);
                break;
            case R.id.ll_my_circle://我的
                if (!isLogin) {
                    dialogConfirm(null, null, null, null).show();
                    return;
                }
                setSelection(1);
                break;
        }
    }

    private void setSelection(int index) {
        FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
        hideFragment(fragmentTransaction);
        switch (index) {
            case 0://
                setSelected(flInterest);
                if (interestCircleFragment == null) {
                    interestCircleFragment = new InterestCircleFragment();
                    Bundle bundle = new Bundle();
                    interestCircleFragment.setArguments(bundle);

                    fragmentTransaction.add(R.id.content_layout, interestCircleFragment, "tab1");
                } else {
                    fragmentTransaction.show(interestCircleFragment);
                }
                fragmentTransaction.commit();
                break;
            case 1://
                setSelected(flMineCircle);
                if (myCircleFragment == null) {
                    myCircleFragment = new MyCircleFragment();
                    Bundle bundle = new Bundle();
                    myCircleFragment.setArguments(bundle);

                    fragmentTransaction.add(R.id.content_layout, myCircleFragment, "tab2");
                } else {
                    fragmentTransaction.show(myCircleFragment);
                }

                fragmentTransaction.commit();
                break;
            case 2://
                setSelected(flNearPeople);
                if (mNearPeopleFragment == null) {
                    mNearPeopleFragment = new NearPeopleFragment();
                    Bundle bundle = new Bundle();
                    mNearPeopleFragment.setArguments(bundle);

                    fragmentTransaction.add(R.id.content_layout, mNearPeopleFragment, "tab3");
                } else {
                    fragmentTransaction.show(mNearPeopleFragment);
                }

                fragmentTransaction.commit();
                break;
        }
    }

    private void setSelected(View view) {
        flInterest.setSelected(false);
        flMineCircle.setSelected(false);
        flNearPeople.setSelected(false);
        view.setSelected(true);
    }

    /**
     * 隐藏所有的fragment
     *
     * @param transaction
     */
    private void hideFragment(FragmentTransaction transaction) {
        if (interestCircleFragment != null) {
            transaction.hide(interestCircleFragment);
        }
        if (myCircleFragment != null) {
            transaction.hide(myCircleFragment);
        }
        if (mNearPeopleFragment != null) {
            transaction.hide(mNearPeopleFragment);
        }
    }
}
